<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/stype.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <a href="javascript:;" class="layui-btn role_add">添加角色</a>
    <table class="layui-table" lay-data="{url:'{{url('admin/getroles')}}', page:true, id:'test2', skin: 'row', even: true}" lay-filter="demo">
        <thead>
        <tr>
            <th lay-data="{type:'checkbox'}"></th>
            <th lay-data="{field:'id', sort: true}">ID</th>
            <th lay-data="{field:'name', templet: '#valid'}">名称</th>
            <th lay-data="{field:'display_name'}">显示名称</th>
            <th lay-data="{field:'description'}">描述</th>
            <th lay-data="{field:'created_at', sort: true}">创建 时间</th>
            <th lay-data="{field:'updated_at', sort: true}">修改时间</th>
            <th lay-data="{field:'wealth', align:'center', toolbar: '#barDemo'}">操作</th>
        </tr>
        </thead>
    </table>
    <input id="handle_status" value="" hidden="hidden">
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery-3.2.1.min.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" value="@{{ d.id }}" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    $(function(){
        $(".role_add").click(function(){
            layer.open({
                title:'添加权限',
                type: 2,
                area: ['700px', '530px'],
                fix: false, //不固定
                maxmin: true,
                content: "roles/create",
                success:function(layero,index){
                },
                end:function(){
                    history.go(0);
                }
            });
        })
    })
</script>
<script>
    layui.use( 'table', function(){
        layer = layui.layer //弹层
            ,table = layui.table
            ,form = layui.form;
        //监听工具条
        table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.post("roles/"+data.id,{'_token':"{{csrf_token()}}",'_method':'delete'},function(e){
                        layer.close(index);
                        if(e == 1){
                            obj.del(); //删除对应行（tr）的DOM结构
                        }else{
                            layer.msg('超级管理不允许删除',{
                                icon: 2,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                            },function(){
                                history.go(0);
                            });
                        }
                    })
                });
            } else if(layEvent === 'edit'){
                layer.open({
                    title:'编辑角色',
                    type: 2,
                    area: ['700px', '530px'],
                    fix: false, //不固定
                    maxmin: true,
                    content: "roles/"+data.id+"/edit",
                    success:function(layero,index){
                    },
                    end:function(){
                        var handle_status = $("#handle_status").val();
                        if ( handle_status == '1' ) {
                            layer.msg('编辑成功！',{
                                icon: 1,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                            },function(){
                                history.go(0);
                            });
                        } else if ( handle_status == '2' ) {
                            layer.msg('编辑失败！',{
                                icon: 2,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                            },function(){
                                history.go(0);
                            });
                        }
                    }
                });

            }
        });
    });
</script>
</body>
</html>